clear
use ElectoralData.dta
log using COUNTERFACTUALAPPROACH.log, replace

******************************************************************************************
***THE MODEL WHERE WE MEASURE THE LEVEL OF STRATEGIC VOTE OF INDIVIDUALS
***  Using the Counterfactual approach    
*****************************************************************************************


*************************************
** STEP 1****
*************************************


**identify and create the SINCERE VOTE using item 44

gen sincerevote=0
replace sincerevote=1 if voto==partidoproximo
replace sincerevote=1 if voto==13


*************************************
** STEP 2****
*************************************


** These statements constraint some coefficients to be zero as explained in the text ***
const define 1 [4]siafiliacpartido=0
const define 2 [6]praticareligion1=0
const define 3 [7]ingresos4=0
const define 4 [7]siafiliacpartido=0
const define 5 [8]ingresos4=0
const define 6 [13]siafiliacpartido=0


** RANDOM UTILITY PREFERENCE MODEL***
**THE ESTIMATION OF THE SINCERE MODEL of the individuals identified as sincere using item 44

asclogit choice ParCercano informacion votoanterior if sincerevote==1, case(id) casevars(ingresos2 ingresos3 ingresos4 /*
*/ estudios praticareligion1 IzqDer evalueconomica evalupolitica siafiliacpartido siafiliacsindicato EDAD sexo) /*
*/ alternatives(alter) const(1,2,3,4,5,6) basealternative(1) technique(bfgs) vce(robust)
* This generates a latex table. It requires outreg2.ado file
outreg2 using counterfactualapproach, tex replace

**predicted probabilities of voting for each party (for all individuals, those who voted strategically and those who did not vote strategically)
predict p0

**GOODNESS OF FIT OF THE MODEL******
predict p1 if e(sample)
** this selects the party that provides the highest utility according to the estimated model
by id: egen pro1=max(p1)
** then, esti1 is equal to 1 if this is the alternative with the higher probability for this individual and 0 otherwise
gen esti1=0
replace esti1=alter if p1==pro1
**the variable "aciertos" takes value 1 if the individual has voted the predicted alternative and 0 otherwise
gen aciertos=0
replace aciertos=1 if voto==esti1 & sincerevote==1
**to measure the number of right predictions we do the following, and we measure the number of observations
sum(aciertos) if aciertos==1

**THE GOODNESS OF FIT FOR EACH PARTY***

**how many voters vote for PSOE?
sum id if voto==alter & voto==1 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==1 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==1 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==1 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==1 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==1 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==1 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==1 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==1 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==1 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==1 & sincerevote==1


**how many voters vote for PP? 
sum id if voto==alter & voto==2 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==2 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==2 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==2  & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==2 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==2 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==2 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==2 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==2 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==2 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==2 & sincerevote==1

**how many voters vote for IU? 
sum id if voto==alter & voto==3 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==3 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==3 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==3 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==3 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==3 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==3 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==3 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==3 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==3 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==3 & sincerevote==1

**how many voters vote for UPyD? 
sum id if voto==alter & voto==4 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==4 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==4 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==4 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==4 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==4 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==4 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==4 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==4 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==4 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==4& sincerevote==1
 

**how many voters vote for CIU? 
sum id if voto==alter & voto==5 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==5 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==5 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==5 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==5 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==5 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==5 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==5 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==5 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==5 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==5 & sincerevote==1


** how many voters vote for ERC? 
sum id if voto==alter & voto==6 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==6 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==6 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==6 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==6 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==6 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==6 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==6 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==6 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==6 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==6 & sincerevote==1

**how many voters vote for PNV?
sum id if voto==alter & voto==7 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==7 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==7 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==7 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==7 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==7 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==7 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==7 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==7 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==7 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==7 & sincerevote==1

**how many voters vote for BNG? 
sum id if voto==alter & voto==8 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==8 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==8 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==8 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==8 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==8 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==8 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==8 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==8 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==8 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==8 & sincerevote==1


**how many voters vote for CC? 
sum id if voto==alter & voto==9 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==9 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==9 & sincerevote==1
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==9 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==9 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==9 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==9 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==9 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==9 & sincerevote==1
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==9 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==9 & sincerevote==1



**how many voters have abstained?
sum id if voto==alter & voto==13 & sincerevote==1
***how many of them are predicted to vote for PSOE?
sum id if esti1==1 & voto==13 & sincerevote==1
***how many of them are predicted to vote for PP?
sum id if esti1==2 & voto==13 & sincerevote==1 
***how many of them are predicted to vote for IU?
sum id if esti1==3 & voto==13 & sincerevote==1
***how many of them are predicted to vote for UPyD?
sum id if esti1==4 & voto==13 & sincerevote==1
***how many of them are predicted to vote for CIU?
sum id if esti1==5 & voto==13 & sincerevote==1
***how many of them are predicted to vote for ERC?
sum id if esti1==6 & voto==13 & sincerevote==1
***how many of them are predicted to vote for PNV?
sum id if esti1==7 & voto==13 & sincerevote==1
***how many of them are predicted to vote for BNG?
sum id if esti1==8 & voto==13 & sincerevote==10
***how many of them are predicted to vote for CC?
sum id if esti1==9 & voto==13 & sincerevote==1
***how many of them are predicted to vote for Abstain?
sum id if esti1==13 & voto==13 & sincerevote==1


**** the total number of individuals
sum(id)


********************************************************************
********************************************************************

**p0 denotes vote probabilities of all individuals and p1 vote probability of sincere individuals
**We have to identify what the strategic voters would have vote, had they voted sincerely
by id: egen pro0=max(p0)
gen esti0=0
replace esti0=alter if p0==pro0 & sincerevote==0



****************************************
**** STEP 3
********************************

**identify SELF-DECLARED STRATEGIC VOTERS (they do not vote for their preferred party + manifest the intention to affect the result of the election)

**self-declared strategic voters among PSOE voters
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto==1

**self-declared strategic voters among PP voters
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto==2


****************************************
**** STEP 4
********************************


** What would have strategic voters of PSOE voted, had they voted sincerely?
**who had voted for PSOE?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto==1 & esti0==1
**who had abstained?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==13
**who had voted for IU?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==3
**who had voted for UpyD?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==4
**who had voted for CIU?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==5
**who had voted for ERC?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==6
**who had voted for PNV?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==7
**who had voted for BNG?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==8
**who had voted for CC?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==9
**who had voted for PP?
sum id if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0 & voto==1 & esti0==2


**What would have strategic voters of PP voted, had they voted sincerely?
**who had voted for PP?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto==2 & esti0==2
**who had abstained?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==13
**who had voted for IU?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==3
**who had voted for UpyD?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==4
**who had voted for CIU?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==5
**who had voted for ERC?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==6
**who had voted for PNV?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==7
**qwho had voted for BNG?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==8
**who had voted for CC?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==9
**who had voted for PSOE?
sum id if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0 & voto==2 & esti0==1



********************************************************************
*** THE LEVEL OF STRATEGIC VOTE using the COUNTERFACTUAL APPROACH
********************************************************************

sum (id) if sincerevote==0 & votoPsoe==6 & esti0~=0 & voto~=esti0
sum (id) if sincerevote==0 & votoPp==6 & esti0~=0 & voto~=esti0

*total number of individuals
sum id if voto==alter

log close



